<template>
  <div class="custom-table-list-wrapper">
    <div class="search" :class="{ active: toggleSearchStatus }">
      <div class="search-left" ref="searchFromRef">
        <el-form class="search-form" inline>
          <el-form-item label="状态">
            <el-select
              class="search-select"
              v-model="searchParams.status"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="(item, index) of dictOptions['status']"
                :key="index"
                :label="item.name"
                :value="Number(item.value)"
                :disabled="item.disabled"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="关键词">
            <el-input
              class="search-input"
              v-model="searchParams.keyword"
              placeholder="请输入名称、编码"
              :suffix-icon="Search"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
        <a
          class="search-left-toggle"
          v-if="showToggle"
          @click="handleToggleSearch"
        >
          <svg-icon :name="toggleSearchStatus ? 'up' : 'down'" />
          {{ toggleSearchStatus ? '收起' : '展开' }}
        </a>
      </div>
      <div class="search-right">
        <el-button type="primary" @click="initTableList(1)"
          ><svg-icon name="search" size="14px" />搜索</el-button
        >
        <el-button @click="handleReset()"
          ><svg-icon name="reset" size="17px" />重置</el-button
        >
      </div>
    </div>
    <div class="content">
      <div class="content-operate">
        <el-button type="primary" @click="handleAdd('新增字典')"
          ><svg-icon name="add" size="15px" />新增字典</el-button
        >
      </div>
      <div class="content-table">
        <el-table class="custom-table" :data="tableData">
          <el-table-column type="index" label="序号" width="60px" />
          <el-table-column prop="name" label="字典名称" />
          <el-table-column prop="code" label="字典编码" />
          <el-table-column prop="remark" label="描述" />
          <el-table-column prop="status" label="状态" width="100">
            <template #default="scope">
              <el-tag :type="getDictStyle('status', scope.row.status)">{{
                getDictValue('status', scope.row.status)
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="200" />
          <el-table-column label="操作" width="120px">
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="handleEdit(scope.row, '编辑字典')"
                >编辑</el-button
              >
              <el-button link type="danger" @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="content-page">
        <el-pagination
          class="custom-pagination"
          :current-page="paginationParams.pageIndex"
          :page-size="paginationParams.pageSize"
          background
          :page-sizes="[10, 20, 30, 50]"
          layout="total, prev, pager, next, sizes, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="initTableList"
        />
      </div>
    </div>
    <AddDialog ref="addRef" :detail="currentDetail" @close="handleClose" />
  </div>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import { useTableList } from '@/hooks/useTableList'
import AddDialog from './add.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useDict } from '@/hooks/useDict'
import { getDictList, dictDelete } from '@/api/manager/systemManage/dict.js'
const {
  dictOptions,
  initListDictOptions,
  initTreeDictOptions,
  getDictValue,
  getDictStyle,
} = useDict()
import { useUserStore } from '@/store/user'
const userStore = useUserStore()

const searchParams = ref({
  status: '',
  keyword: '',
})
const configRef = ref()
const handleConfig = (row) => {
  configRef.value.dialogVisible = true
  currentDetail.value = { ...row }
}
const {
  Search,
  toggleSearchStatus,
  showToggle,
  handleToggleSearch,
  tableData,
  paginationParams,
  total,
  initTableList,
  handleSizeChange,
  handleReset,
  handleDelete,
  addRef,
  detailRef,
  currentDetail,
  handleAdd,
  handleEdit,
  handleDetail,
  handleClose,
  searchFromRef,
} = useTableList(searchParams, getDictList, dictDelete)

const handleChangeStatus = (val) => {
  ElMessage.success('状态修改成功')
}

onMounted(() => {
  initListDictOptions(['status'])
  initTableList(1)
})
</script>
<style lang="scss" scoped>
@import '@/assets/styles/table.scss';
</style>
